Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NumPy 2.x compatibility: replace np.string_ with np.bytes_ #210

Merged
merged 2 commits into from
Jun 21, 2024

Conversation

Z2h-A6n
Copy link
Contributor

@Z2h-A6n Z2h-A6n commented Jun 21, 2024

NumPy v2.0 removed np.string_ from the API and recommended using np.bytes_ instead, so I made that change. Since np.string_ was already an alias for np.bytes_, I think this is backward compatible with recent versions Numpy v1.x. At least test suite passes with both NumPy 2.0.0 and NumPy 1.26.4.

I checked the other API removals in the NumPy v2.0.0 release notes and didn't see any other instances of removed NumPy objects in the nexusformat codebase.

Without this change, some of my previously-working code that checked for certain entries in a nxs.tree.NXdata.attrs dictionary was failing.

NumPy v2.0 removed `np.string_` from the API and recommended using
`np.bytes_` instead. I checked the other API removals in the NumPy v2.0
change log and didn't see any instances of removed objects in the
nexusformat codebase.

See https://numpy.org/doc/stable/release/2.0.0-notes.html for more info.
@rayosborn
Copy link
Contributor

Thanks for the heads-up about NumPy 2.0. It doesn't seem to be available in my conda distribution, but after updating with pip, the pytest does indeed throw up a lot of errors, which your changes fix. Since you have created this PR, please could you add a fix to prevent another deprecation warning. The call signature for the __array_wrap__ function requires a new return_scalar keyword argument, so it should be the following:

    def __array_wrap__(self, value, context=None, return_scalar=False):
        """Transform the array resulting from a ufunc to an NXfield."""
        return NXfield(value, name=self.nxname)

It's on line 3283 of my version of tree.py.

For NumPy 2.0 compatibility.
@Z2h-A6n
Copy link
Contributor Author

Z2h-A6n commented Jun 21, 2024

I updated the __array_wrap__ definition.

Copy link
Contributor

@rayosborn rayosborn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rayosborn rayosborn merged commit f0db0ac into nexpy:main Jun 21, 2024
15 checks passed
@rayosborn
Copy link
Contributor

@Z2h-A6n, thanks for the PR.

@Z2h-A6n
Copy link
Contributor Author

Z2h-A6n commented Jun 21, 2024

No problem, thanks for merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants